#!/bin/sh -e

kmpkgRootDir=$(X= cd -- "$(dirname -- "$0")" && pwd -P)

curDir=$(pwd -P)
echo "kmpkg root directory \033[32m\"$kmpkgRootDir\"\033[0m"
echo "current project directory \033[32m\"$curDir\"\033[0m"

if [ ! -n "$1" ];then
  echo "\033[31merror: you must input project name\033[0m"
  echo "\033[32mUsage:\033[0m"
  echo "\033[33m\t kmcmake <project name>\033[0m"
  exit 1;
fi

if [ -f CMakeLists.txt ];then
  echo "\033[32m$1 already init for cmake\033[0m"
  exit
fi

echo "your project name is \033[32m$1\033[0m"

if [ -d kmncmake_temp_build ];then
  rm -rf kmncmake_temp_build
fi

mkdir kmncmake_temp_build

cmake -S ${kmpkgRootDir}/km-template -B kmncmake_temp_build -DCHANGEME=$1
cmake --build kmncmake_temp_build
cmake --install kmncmake_temp_build --prefix ${curDir}
rm -rf kmncmake_temp_build
